package org.bedracket.bedracketenchantment.enchantment;

import com.mojang.blaze3d.shaders.Effect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;

public class RevertEffectEnchantment extends Enchantment{
    public RevertEffectEnchantment() {
        super(Enchantment.Rarity.RARE, EnchantmentCategory.WEAPON, new EquipmentSlot[]{EquipmentSlot.MAINHAND});
    }

    @Override
    public int getMaxLevel() {
        return 3;
    }

    @Override
    public int getMinCost(int level) {
        return 1 + (level - 1) * 11;
    }

    @Override
    public void doPostAttack(LivingEntity pAttacker, Entity pTarget, int pLevel) {
        if (pTarget instanceof final LivingEntity living) {
            if (living.getActiveEffectsMap().containsKey(MobEffects.GLOWING)) {
                if (living.getActiveEffects().size() > pAttacker.getActiveEffects().size()) {
                    for (MobEffectInstance eachEffect: living.getActiveEffects()) {
                        pAttacker.addEffect(new MobEffectInstance(eachEffect.getEffect(), 5 * 20 * pLevel, pLevel));
                    }
                    living.getActiveEffectsMap().clear();
                } else {
                    for (MobEffectInstance eachEffect: pAttacker.getActiveEffects()) {
                        living.addEffect(new MobEffectInstance(eachEffect.getEffect(), 5 * 20 * pLevel, pLevel));
                    }
                    pAttacker.getActiveEffectsMap().clear();
                }
            }
        }
    }

    @Override
    protected boolean checkCompatibility(Enchantment pOther) {
        return super.checkCompatibility(pOther) && !(pOther instanceof PartyDownEnchantment);
    }
}
